package NM;

import java.util.Stack;

//逆波兰表达式求值；
public class text4 {
    public boolean isOperator(String ch){
        if(ch.equals("+")||ch.equals("-")||ch.equals("*")||ch.equals("/")){
            return true;
        }
        return false;
    }
    public int evalRPN(String[] tokens){
        Stack<Integer> stack=new Stack<>();
       for(String str:tokens){
           if(!isOperator(str)){
               int index=Integer.parseInt(str);
               stack.push(index);
           }else{
               int val2=stack.pop();
               int val1=stack.pop();
               switch (str){
                   case "+":
                       stack.push(val1+val2);
                       break;
                   case "-":
                       stack.push(val1-val2);
                       break;
                   case "*":
                       stack.push(val1*val2);
                       break;
                   case "/":
                       stack.push(val1/val2);
               }
           }
       }
       return stack.pop();
    }
}
